package com.github.cbyzzy.controller;

import com.github.cbyzzy.common.DeviceTypeEnum;
import com.github.cbyzzy.criteria.HTCriteria;
import com.github.cbyzzy.entity.ResultEntity;
import com.github.cbyzzy.entity.ResultUtils;
import com.github.cbyzzy.model.domain.DeviceEventInfo;
import com.github.cbyzzy.model.entity.DeviceEvent;
import com.github.cbyzzy.model.mapper.DeviceEventMapper;
import com.github.cbyzzy.model.query.DeviceQuery;
import com.github.cbyzzy.service.DeviceEventService;
import com.github.cbyzzy.utils.DateUtils;
import com.github.cbyzzy.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Api(tags = "设备事件")
@RestController
@Slf4j
@RequestMapping(value = "/deviceEvent", produces = {"application/json;charset=utf-8"})
public class DeviceEventController {

    @Autowired
    private DeviceEventService deviceEventService;

    @Autowired
    DeviceEventMapper deviceEventMapper;

    @ApiOperation("获取摄像机报警事件")
    @GetMapping("/list")
    public ResultEntity deviceEventList(DeviceQuery deviceQuery) {
        HTCriteria<DeviceEvent> criteria = HTCriteria.getInstance(DeviceEvent.class);
        criteria.andEqualTo(DeviceEvent::getType, DeviceTypeEnum.DEVICE_TYPE_CAM.getCode());
        if(StringUtils.isNotBlank(deviceQuery.getIp())){
            criteria.andEqualTo(DeviceEvent::getIp,deviceQuery.getIp());
        }
        criteria.orderDesc(DeviceEvent::getCreateTime);
        List<DeviceEvent> deviceEvents = deviceEventService.find(criteria);
        return ResultUtils.ok(deviceEvents);

    }


    @ApiOperation("报警事件统计")
    @GetMapping("/statistics")
    public ResultEntity deviceEventStatisticsList() {

        Date date = DateUtils.startOfMonth();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        List<DeviceEventInfo> deviceEventStatisticsList = deviceEventMapper.getDeviceEventStatisticsList(time);
        return ResultUtils.ok(deviceEventStatisticsList);

    }


}
